package com.rock.code.system.login.controller;

import com.rock.code.base.controller.BaseController;
import com.rock.code.base.domain.AjaxJson;
import com.rock.code.base.domain.Constants;
import com.rock.code.common.exceptions.SystemException;
import com.rock.code.system.login.service.LoginService;
import com.rock.code.system.user.domain.TbSysLoginmsg;
import org.apache.ibatis.io.Resources;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.ServletOutputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.Map;
import java.util.Properties;

/**
 * The type Login controller.
 *
 * @author kongqiang
 * @date 2017.12.29
 */
@Controller
@RequestMapping("/login")
public class LoginController extends BaseController {

    /**
     * The Login service.
     */
    @Resource
    private LoginService loginServiceImpl;

    /**
     * Dispatch.
     *
     * @return the string
     * @throws Exception the exception
     * @author kongqiang
     * @date 2017.12.29
     */
    @RequestMapping("")
    public String dispatch() throws Exception {
        loginServiceImpl.goLoginPage(request);
        return "system/login/login";
    }

    /**
     * User login.
     *
     * @return the ajax json
     * @throws Exception the exception
     * @author kongqiang
     * @date 2017.12.29
     */
    @ResponseBody
    @RequestMapping(value = "/userLogin", method = RequestMethod.POST)
    public AjaxJson userLogin() throws Exception {
        Map<String, Object> param = getRequestMapSingle();
        try {
            ajaxJson = loginServiceImpl.userLogin(request, response, param);
        } catch (Exception e){
            e.printStackTrace();
            throw new SystemException();
        }

        return ajaxJson;
    }

    /**
     * Generate captcha.
     *
     * @author kongqiang
     * @date 2017.12.29
     */
    @RequestMapping(value = "/generateCaptcha", method = RequestMethod.GET)
    public void generateCaptcha() {
        ByteArrayOutputStream output = new ByteArrayOutputStream();
        loginServiceImpl.generateCaptcha(request, output);
        try {
            ServletOutputStream out = response.getOutputStream();
            output.writeTo(out);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * User login.
     *
     * @return the ajax json
     * @throws Exception the exception
     * @author wangwenbo
     * @date 2018.01.23
     */
    @ResponseBody
    @RequestMapping(value = "/getIdentifyCode", method = RequestMethod.GET)
    public AjaxJson getIdentifyCode() throws Exception {
        try {
            String identifyCode = (String) session.getAttribute(Constants.SESSION_VALIDATECODE);
            ajaxJson.setSuccess(true);
            ajaxJson.setResult(identifyCode);
        } catch (Exception e){
            e.printStackTrace();
            throw new SystemException();
        }
        return ajaxJson;
    }

    /**
     * User login.
     *
     * @return the ajax json
     * @throws Exception the exception
     * @author wangwenbo
     * @date 2018.01.23
     */
    @ResponseBody
    @RequestMapping(value = "/logout", method = RequestMethod.GET)
    public void logout() throws Exception {
        String logout_link = "/login";
        try {
            Properties loginProps = new Properties();
            loginProps.load(Resources.getResourceAsStream("login.properties"));
            String enableSSO = loginProps.getProperty("enableSSO");
            if(enableSSO.equals("true")) {
                String loginUrl = loginProps.getProperty("loginUrl");
                String logoutUrl = loginUrl.replace("login", "logout");
                String portalUrl = loginUrl.replace("/cas/login", "/portal/");
                logout_link = logoutUrl + "?url=" + portalUrl;
                logout_link = loginProps.getProperty("logoutUrl", logout_link);
            }
            session.invalidate();
        } catch (Exception e) {
            e.printStackTrace();
            throw new SystemException();
        }
        response.sendRedirect(logout_link);
    }

}
